package cn.qdu.service;

import cn.qdu.dao.TopicDao;
import cn.qdu.model.Topic;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TopicService {
    private TopicDao topicDao = new TopicDao();

    public List<Topic> getHotTopics(int limit) {
        try {
            return topicDao.getHotTopics(limit);
        } catch (SQLException e) {
            e.printStackTrace();
            return getDefaultHotTopics(); // 降级方案
        }
    }

    private List<Topic> getDefaultHotTopics() {
        // 数据库不可用时的默认数据
        List<Topic> topics = new ArrayList<>();
        topics.add(new Topic("default-1", "热门话题", 100, 500));
        topics.add(new Topic("default-2", "技术讨论", 80, 400));
        return topics;
    }

}